package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.kaishengit.entity.Files;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.Page;
import com.kaishengit.util.RowMapper;
public class FilesDao {
	private DBHelp<Files> db = new DBHelp<Files>();
	/**
	 * 根据项目ID查找所有相关的文件夹
	 * @param projectid
	 * @param page
	 * @return
	 */
	public Page<Files> findAllFiles(String projectid,Page<Files> page) {
		StringBuilder sql = new StringBuilder();
		 sql.append("SELECT tf.id ,tf.f_name,tf.t_user_id,tf.t_project_id ,tu.username,");
		 sql.append("(SELECT COUNT(*) FROM t_file WHERE t_files_id = tf.id) AS totalNum,");
		 sql.append("(SELECT SUM(size) FROM t_file WHERE t_files_id = tf.id) AS totalSize FROM t_files AS tf ");
		 sql.append("LEFT JOIN t_user AS tu ON tf.t_user_id = tu.id ");
		 sql.append("WHERE tf.t_project_id =?  order by tf.createtime desc LIMIT ?,?");
		 List<Files> list =  db.executeQueryForList(sql.toString(), new FilesRowMapper(), projectid,page.getOffest(),page.getPageSize());
		 page.setList(list);
		 return page;
	}

	/**
	 * 得到用户参与项目的文件夹得总记录数
	 * @param projectid
	 * @return
	 */
	public long getCount(String projectid) {
		String sql = "select count(*) from t_files where t_project_id = ?";
		return (Long) db.executeForFunction(sql, projectid);
	}
	/**
	 * 根据创建文件夹的名字，查找此项目中的是否已有了这个文件夹的名字
	 * @param name
	 * @param projectid
	 * @return
	 */
	public Files findByName(String name,String projectid) {
		String sql = "select id,f_name,t_user_id,t_project_id from t_files where t_project_id = ? and f_name = ?";
		return db.executeQueryForObject(sql, new FilesRowMapper(), projectid,name);
	}
	/**
	 * 保存新创建的这个文件夹
	 * @param files
	 */
	public void saveFiles(Files files) {
		String sql = "insert into t_files(id,f_name,t_user_id,t_project_id,createtime ) values (?,?,?,?,?)";
		db.executeSQL(sql, files.getId(),files.getFilesname(),files.getUserid(),files.getProjectid(),files.getCreatime());
	}
	private class FilesRowMapper implements RowMapper<Files>{
		
		public Files mapRow(ResultSet rs) throws SQLException {
			Files files = new Files();
			files.setId(rs.getString("id"));
			files.setFilesname(rs.getString("f_name"));
			files.setUserid(rs.getString("t_user_id"));
			files.setProjectid(rs.getString("t_project_id"));
			files.setSize(rs.getInt("totalSize"));
			files.setTotalNum(rs.getInt("totalNum"));
			
			User user = new User();
			user.setId(rs.getString("t_user_id"));
			user.setUsername(rs.getString("username"));
			files.setUser(user);
			
		/*	Project p = new Project();
			p.setId(rs.getString("t_project_id"));
			files.setProject(p);*/
			return files;
		}
		
	}
private class FilesRowMapper2 implements RowMapper<Files>{
		
		public Files mapRow(ResultSet rs) throws SQLException {
			Files files = new Files();
			files.setId(rs.getString("id"));
			files.setFilesname(rs.getString("f_name"));
			files.setUserid(rs.getString("t_user_id"));
			files.setProjectid(rs.getString("t_project_id"));
			return files;
		}
		
	}
	public Files findById(String id) {
		String sql = "select id,f_name,t_user_id,t_project_id  from t_files where id = ? ";
		return db.executeQueryForObject(sql, new FilesRowMapper2(), id);
		
	}

	public Object editName(String id, String name, Files files) {
		String sql = "update t_files set f_name=?,t_project_id=?,t_user_id=? where  id = ?";
		return db.executeSQL(sql, name,files.getProjectid(),files.getUserid(),id);
	}

	public void delById(String id) {
		String sql = "delete from t_files where id = ?";
		db.executeSQL(sql, id);
		
	}
	
}
